<?php
/**
 * Aes 加解密服务层
 * @author maniac
 */
namespace app\common\service;

class Aes
{
    // 加解密密钥
    private $aesKey = null;
	private $iv     = null;
	private $cipher = 'AES-256-CBC';
    
    public function __construct()
    {
        $this->aesKey = md5(config('site.aes_key'));
        $ivLen        = openssl_cipher_iv_length($this->cipher);
		$this->iv     = substr($this->aesKey, 0, $ivLen);
    }
    
    /**
     * 加密字符串
     * @param  string input 要加密的字符串
     * @return string
     */
    public function encrypt($input)
    {
        return base64_encode(openssl_encrypt($input, $this->cipher, $this->aesKey, OPENSSL_RAW_DATA, $this->iv));
    }
    
    /**
     * 解密字符串
     * @param  string $input 要解密的字符串
     * @return string
     */
    public function decrypt($input)
    {
        return openssl_decrypt(base64_decode($input), $this->cipher, $this->aesKey, OPENSSL_RAW_DATA, $this->iv);
    }
}
